* 1: Council-level estimates
* Table 3, A12, A13, A15
gl data ".../Replication Files/data"




* Merge LGD with the 2001 and 2011 Censuses
{
* 1 - Prep LGD
{
import excel "$data/Gujarat_LGD.xlsx", sheet("Sheet1") firstrow clear
ren SNo lgd_id


egen gp_id = group(DistrictName BlockName PanchayatName)

* Quick Clean
foreach x in DistrictName BlockName PanchayatName VillageName {
replace `x' = upper(subinstr(`x'," ","",.))
replace `x' = subinstr(`x',"G.P.","",.)
replace `x' = subinstr(`x',")","",.)
replace `x' = subinstr(`x',"-","",.)
replace `x' = subinstr(`x',"(","",.)
replace `x' = subinstr(`x',".","",.)
}
replace BlockName="DOHAD" if DistrictName=="DOHAD" & BlockName=="DAHOD"

duplicates tag DistrictName BlockName PanchayatName VillageName, gen(tag)
drop if tag>0
drop tag
drop State
}
save "$data/gp_lgd.dta", replace




* 2 - Prep Census 2011
{
import excel "$data/Census_2011.xlsx", firstrow clear allstring
ren VillageCode VillageCensusCode2011
keep DistrictName CDBlockName GramPanchayatName VillageName VillageCensusCode2011 TotalPopulation
ren TotalPopulation T_P_2011

ren CDBlockName BlockName
ren GramPanchayatName PanchayatName

foreach x in DistrictName BlockName PanchayatName VillageName {
replace `x' = upper(subinstr(`x'," ","",.))
replace `x' = subinstr(`x',"(OG)","",.)
replace `x' = subinstr(`x',"(CT)","",.)
replace `x' = subinstr(`x',"DETROJ-RAMPURA","DETROJ",.)
replace `x' = subinstr(`x',")","",.)
replace `x' = subinstr(`x',"-","",.)
replace `x' = subinstr(`x',"(","",.)
replace `x' = subinstr(`x',"NO.","",.)
replace `x' = subinstr(`x',".","",.)
replace `x' = subinstr(`x',"","",.)
}

duplicates tag DistrictName BlockName PanchayatName VillageName, gen(tag)
drop if tag>0
drop tag

gen lbm_id = _n
}
save "$data/census2011.dta", replace




* 3 - Fuzzy Merge LGD and Census 2011 
{
use "$data/gp_lgd.dta", clear
reclink DistrictName BlockName PanchayatName VillageName using "$data/census2011.dta", gen(score) idm(lgd_id) idu(lbm_id)

* Keep best matches
replace score=0 if score==.
bysort lbm_id: egen max_score = max(score)
replace lbm_id=. if score!=max_score

* Remove duplicate matches
duplicates tag lgd_id, gen(tag)
replace lbm_id=. if tag>0 
drop tag
duplicates drop lgd_id, force

keep lgd_id DistrictName BlockName PanchayatName VillageName gp_id lbm_id VillageCensusCode2011 T_P_2011
}
save "$data/vill_lgd_census11.dta", replace




* 4 - Merge Census 2001 onto vill_lgd_census11.dta
{
import excel "$data/LocalbodyMapping.xlsx", sheet("Report") cellrange(A4:N19253) firstrow allstring clear
keep VillageCode VillageCensusCode2001

ren VillageCode VillageCensusCode2011
drop if VillageCensusCode2011=="" | VillageCensusCode2001==""
duplicates drop VillageCensusCode2011 VillageCensusCode2001, force

merge 1:m VillageCensusCode2011 using "$data/vill_lgd_census11.dta", gen(merge)
drop if merge==1
drop merge

order lgd_id DistrictName BlockName PanchayatName VillageName gp_id VillageCensusCode2011 VillageCensusCode2001 lbm_id T_P_2011

ren VillageCensusCode2001 V_CT_CODE
merge m:1 V_CT_CODE using "$data/census2001.dta", gen(merge) keepusing(T_P SC_P ST_P T_F)
ren V_CT_CODE VillageCensusCode2001
keep if merge!=2
drop merge

destring T_P_2011, replace

foreach var in VillageCensusCode2001 VillageCensusCode2011 {
replace `var'="" if lbm_id==.
}
replace T_P=. if lbm_id==.

gen missing_vill = lbm_id==.
bysort gp_id: egen missing_gp = sum(missing_vill) 
bysort gp_id: egen gp_pop=sum(T_P)
bysort gp_id: egen gp_sc_pop=sum(SC_P)
bysort gp_id: egen gp_st_pop=sum(ST_P)
gen gp_scst = gp_sc_pop+gp_st_pop
gen prop_scst = gp_scst/gp_pop
replace gp_pop=. if missing_gp>0
}
*
}
save "$data/vill_lgd_census11_01.dta", replace




* Table 3, Columns 2-3
{
use "$data/Samras_GPs.dta", clear
keep lgd_id RsInLacs ExcelSheetName
keep if lgd_id!=.

merge 1:1 lgd_id using "$data/vill_lgd_census11_01.dta", gen(merge)

gen samr = merge==3
gen fem_samr = samr==1 & strpos(Excel,"Mahila")!=0
drop merge

bysort gp_id: egen samras=sum(samr)
bysort gp_id: egen fem_samras=sum(fem_samr)
bysort gp_id: egen grant_samras=sum(Rs)

replace samras = samras>0
replace fem_samras = fem_samras>0
gen gen_samras = samras - fem_samras
duplicates drop gp_id, force



* Panel 1: Cutoff = 5001
foreach b in 5001 {
foreach out in gen_samras fem_samras {
di "cutoff `b'   `out' "

qui rdrobust `out' gp_pop if inrange(gp_pop,`b'-1000,`b'+999), c(`b') all vce(cluster gp_pop) bwselect(msetwo)
di "b " round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "

qui sum `out' if inrange(gp_pop,`b'-1000,`b'+999)
di "mean: " round(r(mean),.001)
di "obs:" r(N)
di ""
}
di ""
}
*



* Panel 2: Cutoffs = 4001, 6001
* 4001 estimate scaled by 9/11; 6001 estimate scaled by 13/11
{
gen c = 4001 
replace c = 6001 if gp_pop>=5001

gen bwselect = "msetwo" in 1
replace bwselect = "msetwo" in 2

gen vce = "cluster gp_pop" in 1
replace vce = "cluster gp_pop" in 2

foreach out in gen_samras fem_samras {

qui rdmc `out' gp_pop if inrange(gp_pop,3001,7000), c(c) bwselect(bwselect) vce(vce)
lincom (c1*9/11 + c2*13/11)/2

qui sum `out' if inrange(gp_pop,3001,7000)
di "mean: " round(r(mean),.001)
di "obs:" r(N)
}
}
*



* Panel 3: Cutoff = 8001
* Estimate scaled by 17/11
foreach b in 8001 {

foreach out in gen_samras fem_samras {
di "cutoff `b'   `out' "

qui rdrobust `out' gp_pop if inrange(gp_pop,`b'-1000,`b'+999), c(`b') all  vce(cluster gp_pop) bwselect(msetwo)
di "b" round(e(tau_bc)*17/11,.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb)*17/11,.01) " "  round(e(ci_r_rb)*17/11,.01) "]   "

qui sum `out' if inrange(gp_pop,`b'-1000,`b'+999)
di "mean: " round(r(mean),.001)
di "obs:" r(N)
di ""
}
di ""
}
*

}
*



* Table A12, Panels 1-2
foreach out in gen_samras fem_samras {
use "$data/Samras_GPs.dta", clear
keep lgd_id RsInLacs ExcelSheetName
keep if lgd_id!=.

merge 1:1 lgd_id using "$data/vill_lgd_census11_01.dta", gen(merge)

gen samr = merge==3
gen fem_samr = samr==1 & strpos(Excel,"Mahila")!=0
drop merge

bysort gp_id: egen samras=sum(samr)
bysort gp_id: egen fem_samras=sum(fem_samr)
bysort gp_id: egen grant_samras=sum(Rs)

replace samras = samras>0
replace fem_samras = fem_samras>0
gen gen_samras = samras - fem_samras
duplicates drop gp_id, force


local b = 5001


* Columns 1-2
foreach kernel in uni epa {
qui rdrobust `out' gp_pop if inrange(gp_pop,`b'-1000,`b'+999), c(`b') all  vce(cluster gp_pop) kernel(`kernel')
di "`b' `out' kernel `kernel' " round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "
}
*


* Columns 3-4
forv p = 2/3 {
qui rdrobust `out' gp_pop if inrange(gp_pop,`b'-1000,`b'+999), c(`b') all  vce(cluster gp_pop) p(`p')
di "`b' `out' poly `p' " round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "
}
*


* Column 5
{
qui rdrobust `out' gp_pop if inrange(gp_pop,`b'-1000,`b'+999), c(`b') all  vce(cluster gp_pop)
di "`b'  `out' no controls " round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "
}
*


* Column 6
{
qui rdrobust `out' gp_pop if inrange(gp_pop,`b'-1000,`b'+999), c(`b') all  vce(cluster gp_pop)
di "`b' `out' incl. outliers " round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "
}
*

}
*



* Table A13, Panels 1-2
{
use "$data/Samras_GPs.dta", clear
keep lgd_id RsInLacs ExcelSheetName
keep if lgd_id!=.

merge 1:1 lgd_id using "$data/vill_lgd_census11_01.dta", gen(merge)

gen samr = merge==3
gen fem_samr = samr==1 & strpos(Excel,"Mahila")!=0
drop merge

bysort gp_id: egen samras=sum(samr)
bysort gp_id: egen fem_samras=sum(fem_samr)
bysort gp_id: egen grant_samras=sum(Rs)

replace samras = samras>0
replace fem_samras = fem_samras>0
gen gen_samras = samras - fem_samras
duplicates drop gp_id, force



local b = 5001
foreach out in gen_samras fem_samras {
foreach bw in mserd msetwo msesum msecomb2 cerrd certwo cersum cercomb2 {
di "cutoff `b'   `out'  `bw'"

qui rdrobust `out' gp_pop if inrange(gp_pop,`b'-1000,`b'+999), c(`b') all  vce(cluster gp_pop) bwselect(`bw')
di "b " round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "

di ""
}
}
*

}
*



* Table A15, Panels 1-2
{
use "$data/Samras_GPs.dta", clear
keep lgd_id RsInLacs ExcelSheetName
keep if lgd_id!=.

merge 1:1 lgd_id using "$data/vill_lgd_census11_01.dta", gen(merge)

gen samr = merge==3
gen fem_samr = samr==1 & strpos(Excel,"Mahila")!=0
drop merge

bysort gp_id: egen samras=sum(samr)
bysort gp_id: egen fem_samras=sum(fem_samr)
bysort gp_id: egen grant_samras=sum(Rs)

replace samras = samras>0
replace fem_samras = fem_samras>0
gen gen_samras = samras - fem_samras
duplicates drop gp_id, force


* RD Multiple Cutoffs (rdmc)
expand 2
bysort gp_id: gen copy = _n

gen 	c = 4001 if inrange(gp_pop,3001,5000) & copy==1
replace c = 6001 if inrange(gp_pop,5001,7000) & copy==1
replace c = 8001 if inrange(gp_pop,7001,9000) & copy==1

replace c = 5001 if inrange(gp_pop,4001,6000) & copy==2
replace c = 7001 if inrange(gp_pop,6001,8000) & copy==2
replace c = 9001 if inrange(gp_pop,8001,10000) & copy==2

gen bwselect = "msetwo" in 1
forv n = 2/6 {
replace bwselect = "msetwo" in `n'
}

gen vce = "cluster gp_pop" in 1
forv n = 2/6 {
replace vce = "cluster gp_pop" in `n'
}

foreach out in gen_samras fem_samras {

qui rdmc `out' gp_pop, c(c) bwselect(bwselect) vce(vce)

* Column 1
lincom c2

* Column 2
lincom c2 - (c1 + c3)/2 - (c5 - (c4 + c6)/2)

* Column 3
lincom c2 - (c1*9/11 + c3*13/11)/2 - (c5*17/11 - (c4*15/11 + c6*19/11)/2)

}
*
}
*
